package CCRMServer.Controllers;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import CCRMServer.DBConnect;
import Common.SERVER_COMMANDS;
import Common.ServerAnswer;
import Common.Entities.Sale;
import Common.Entities.SalesmenAndCustomersForSale;


public class ServerEmployeeManager {

	/**
	 * this method is returning ServerAnswer of 2 ArrayList of salesman and customers for a given template.
	 * @param sale
	 * @return
	 */
	public static ServerAnswer showSalesmenAndCustomers(Sale sale){
		Connection conDB = DBConnect.mySQLConnection();
		ResultSet res = null;
		try {
			res = DBConnect.selectWithFilter(conDB, "Employees", "employeeID, employeeName" , "position = 'Sales man' ");
			ArrayList<String[]> salesmen = new ArrayList<String[]>();
			String[] salesman = new String[2];
			while(res.next()){
				salesman[1] = res.getString(1);
				salesman[2] = res.getString(2);
				salesmen.add(salesman);
			}
			ArrayList<String[]> customers = ServerCustomerManagement.showCustomersInTemplate(sale.getTemplateID());
			SalesmenAndCustomersForSale data = new SalesmenAndCustomersForSale(sale, salesmen, customers);
			return new ServerAnswer(SERVER_COMMANDS.SHOW_SALESMEN_AND_CUSTOMERS, data);
		} catch (SQLException e) {
			return  new ServerAnswer (SERVER_COMMANDS.SHOW_SALESMEN_AND_CUSTOMERS, null, "DB failure" );
		}
	}

}
